<?php

namespace app\airobot\controller;

use app\airobot\CommonLogic;
use app\airobot\model\OriginBattleConfig;
use app\airobot\model\OriginRobotSpinConfig;
use think\admin\Controller;
use think\facade\Db;
use ZipArchive;

/**
 * 超管系统-Battle测试
 * Class Banner
 * @package app\airobot\controller
 */
class TestBattle extends Controller
{
    private $table = 'rt_battle_exe_config';
    public $commonLogic;

    /**
     * Spin测试
     * @auth true
     */
    public function index()
    {
        $this->commonLogic = new CommonLogic();
        $this->title = 'Battle测试';
        $query = $this->_query($this->table);
        $where = [];
        $query->where($where);
        $query->order('id desc')->page();
    }

    protected function _index_page_filter(array &$data)
    {
        $this->commonLogic = new CommonLogic();
        foreach ($data as &$vo) {
            $vo['pool_init_amount'] = $this->commonLogic->DealAmount($vo['pool_init_amount']);
        }
    }

    /**
     * 创建盲盒任务
     * @auth true
     */
    public function addbattle()
    {
        if ($this->request->isPost()) {
            Db::startTrans();
            $data = $this->request->post();

            // 参数校验
            if (!is_numeric($data["total_user_count"]) || $data["total_user_count"] > 4 || $data["total_user_count"] <= 0) {
                $this->error("总人数范围: >0 & <=4");
            }
            if (!is_numeric($data["real_user_count"]) || $data["real_user_count"] > 4 || $data["real_user_count"] <= 0) {
                $this->error("真人人数范围: >0 & <=4");
            }
            if ($data["real_user_count"] > $data["total_user_count"] ) {
                $this->error("真人人数不能大于总人数");
            }
            if ($data["box_ids"] == "") {
                $this->error("盒子ID不能为空");
            }
            if (!is_numeric($data["total_battle_times"]) || $data["total_battle_times"] == 0) {
                $this->error("执行次数不对");
            }
            if (!is_numeric($data["pool_init_amount"])) {
                $this->error("水池初始值不对");
            }
            $boxIds = explode("|", $data["box_ids"]);
            $boxModel = new \app\airobot\model\Box();
            $boxList = $boxModel->queryList([], ["field" => "id", "data" => $boxIds], [], [], [], "", $field = "*", $order = "id asc");
            if (count($boxList) != count($boxIds)) {
                $this->error("未找到盲盒ID");
            }

            // 创建任务配置
            $createData["mode"] = $data["mode"];
            $createData["real_user_count"] = $data["real_user_count"];
            $createData["robot_user_count"] = $data["total_user_count"] - $data["real_user_count"];
            $createData["total_user_count"] = $data["total_user_count"];
            $createData["box_ids"] = $data["box_ids"];
            $createData["total_battle_times"] = $data["total_battle_times"];
            $createData["crt_battle_times"] = 0;
            $createData["pool_init_amount"] = $data["pool_init_amount"];
            $createData["is_init_pool"] = $data["is_init_pool"];
            $createData["start_time"] = 0;
            $createData["use_seconds"] = 0;
            $createData["status"] = 1;
            Db::table("rt_battle_exe_config")->insert($createData);

            // 如果一切正常，提交事务
            Db::commit();

            $this->success("保存成功！");
        } else {
            OriginBattleConfig::mForm('formbattle');
        }
    }

    /**
     * 查看结果
     * @auth true
     */
    public function queryresult()
    {
        $battleConfigModel = new \app\airobot\model\RtBattleExeConfig();
        $vo = $battleConfigModel->queryInfo(["id" => $_GET["id"]]);
        if ($vo['result_json'] == "") {
            $this->success("未执行完成！");
        }

        // 将 JSON 字符串解析为 PHP 对象或数组
        $paramsJson = json_decode($vo['result_json'], true);
        ksort($paramsJson);
        $formattedJsonString = json_encode($paramsJson, JSON_PRETTY_PRINT);
        $vo['result_json'] = $formattedJsonString;

        $this->vo = $vo;
        OriginBattleConfig::mForm('result');
    }
}